﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Z1029.NoteEditor
{
    public class MusicPlayerCtrl : MonoBehaviour
    {
        public Joystick timeCtrl;
        public Slider timeScale;
        public Slider progressBar;
        public InputField time;
        public InputField beat;
        public Toggle play;
        public Toggle playWhenChange;
        public bool changing;
        int lastTime = int.MinValue;
        float lastProgress = 0;
        void Start()
        {
            time.text = LevelController.instance.NowTime.ToString();
            beat.text = LevelController.instance.NowBeatTime.ToString();
            UpdateProgress();
        }

        void Update()
        {
            if (LevelController.instance.Play)
                UpdateProgress();
            if (timeCtrl.value != 0)
            {
                if (timeCtrl.value > 0)
                {
                    if (Utils.GetTimeFromBeatTime(LevelController.instance.NowBeatTime + (int)(timeCtrl.value * 10), LevelController.instance.judgelines[0].bpm) < LevelController.instance.music.length)
                        LevelController.instance.NowBeatTime += (int)(timeCtrl.value * 10);

                }
                else
                {
                    if (LevelController.instance.NowBeatTime + (int)(timeCtrl.value * 10) >= 0)
                        LevelController.instance.NowBeatTime += (int)(timeCtrl.value * 10);
                }
            }
            if (lastTime != LevelController.instance.NowBeatTime)
            {
                lastTime = LevelController.instance.NowBeatTime;
                beat.text = LevelController.instance.NowBeatTime.ToString();
                time.text = Utils.GetTimeFromBeatTime(LevelController.instance.NowBeatTime, LevelController.instance.judgelines[0].bpm).ToString();
            }
            if(lastProgress==progressBar.value)
            {
                changing = false;
            }
        }

        public void UpdateProgress()
        {
            progressBar.value = (float)LevelController.nowTime / LevelController.instance.music.length * 4096;
        }

        public void ChangeProgress()
        {
            if(playWhenChange.isOn)changing = true;
            if (!LevelController.instance.Play)
            {
                if (lastProgress != progressBar.value)
                {
                    if (!LevelController.instance.musicSource.isPlaying)
                    {
                        LevelController.instance.musicSource.time = (float)LevelController.nowTime;
                        if(playWhenChange.isOn)LevelController.instance.musicSource.Play();
                    }
                }
                else
                {
                    //LevelController.instance.musicSource.Pause();
                    changing = false;
                }
                float delta = (progressBar.value - lastProgress) * LevelController.instance.music.length;
                delta /= 4096;
                delta = delta * (1/Time.deltaTime);
                LevelController.instance.musicSource.pitch = 1;

                float num = progressBar.value / 4096f * LevelController.instance.music.length;
                LevelController.instance.NowBeatTime = Utils.GetBeatTimeFromTime(num, LevelController.instance.judgelines[0].bpm);
                lastProgress = progressBar.value;
            }
        }

        public void PlayPause()
        {
            LevelController.instance.Play = play.isOn;
        }

        public void ChangeTimeScale()
        {
            //print("000");
            LevelController.instance.TimeScale = timeScale.value;
        }

        public void ChangeTime()
        {
            bool b = LevelController.instance.Play;
            LevelController.instance.Play = false;
            if (Utils.GetBeatTimeFromTime(float.Parse(time.text), LevelController.instance.judgelines[0].bpm) >= 0 && float.Parse(time.text) < LevelController.instance.music.length)
                LevelController.instance.NowBeatTime = Utils.GetBeatTimeFromTime(float.Parse(time.text), LevelController.instance.judgelines[0].bpm);
            LevelController.instance.Play = b;

            UpdateProgress();
            print(LevelController.instance.NowBeatTime);
        }

        public void ChangeBeat()
        {
            bool b = LevelController.instance.Play;
            LevelController.instance.Play = false;
            if (int.Parse(beat.text) >= 0 && Utils.GetTimeFromBeatTime(int.Parse(beat.text), LevelController.instance.judgelines[0].bpm) < LevelController.instance.music.length)
                LevelController.instance.NowBeatTime = int.Parse(beat.text);
            LevelController.instance.Play = b;

            UpdateProgress();
        }
    }
}